package twoD.hofem;

/**
 * A basis for polynomials of degree p where the basis functions are Lagrange
 * polynomials up to p.
 * 
 * @author White Horse
 */
public class LagrangeFB extends AbstractPolynomialBasisOnR {

	/**
	 * Constructs a Lagrange polynomial basis of degree 2.
	 */
	public LagrangeFB() {

		setP(2);
	}

	/**
	 * Constructs a Lagrange polynomial basis of the specified degree.
	 * 
	 * @param p
	 *            polynomial degree
	 */
	public LagrangeFB(int p) {
		setP(p);
	}

	@Override
	public FunctionRToR[] createBasis(int p) {

		PolynomialRToR[] shapeFunctions = new PolynomialRToR[p + 1];
		double[] knot = new double[p + 1];
		double pp = p;
		for (int i = 0; i < p + 1; i++) {
			knot[i] = -1 + 2 * i / pp;
		}

		for (int j = 0; j < p + 1; j++) {
			shapeFunctions[j] = new PolynomialRToR(-1, 1, 1);
			for (int i = 0; i < p + 1; i++) {
				if (j == i) {
				} else {
					shapeFunctions[j] = shapeFunctions[j]
							.multiply(new PolynomialRToR(-1, 1, -knot[i], 1)
									.multiply(1 / (knot[j] - knot[i])));
				}
			}
		}
		return shapeFunctions;
	}

	@Override
	public String getName() {
		return "Lagrange Polynomials";
	}

	public Interval getDomain() {
		return new Interval(-1, 1);
	}
}
